/**
 * 
 */
package ar.edu.unq.agenda.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.unq.edu.agenda.domain.Contact;
import ar.unq.edu.agenda.home.Agenda;

/**
 * @author nicolas
 *
 */
public class ListContacts extends HttpServlet {

	
	/** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.processRequest(request, response);
    }
    
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		
		List<Contact> contacts = Agenda.getHome(Contact.class).allInstances();
        request.getSession().setAttribute("contacts", contacts);
        response.setCharacterEncoding("UTF-8");
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/agenda/contactlist.jsp");
        dispatcher.include(request, response);
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.processRequest(request, response);
    }
        
	
}

